package com.example.fitapp.activity.chart;

import com.example.fitapp.activity.bean.BodyDataInfo;
import com.example.fitapp.activity.utils.DateUtil;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.LimitLine;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.ValueFormatter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MyChart {

    public final LineChart lineChart ;
    private XAxis xAxis; //X轴
    private YAxis leftYAxis; //左侧Y轴
    private YAxis rightYAxis;//右侧Y轴
    private Legend legend;              //图例
    private LimitLine limitLine;        //限制线
    public MyChart(LineChart lineChart){
        this.lineChart = lineChart;
    }
    public void initChart(){
        /***图表设置***/
        //是否展示网格线
        lineChart.setDrawGridBackground(false);
        lineChart.setBackgroundColor(0xFF3e3e4e);
        //是否显示边界
        lineChart.setDrawBorders(false);
        //是否可以拖动
        lineChart.setDragEnabled(false);
        //是否有触摸事件
        lineChart.setTouchEnabled(true);

        //设置XY轴动画效果
//        lineChart.animateY(2500);
        lineChart.animateX(1500);
        /*XY轴的设置*/
        xAxis = lineChart.getXAxis();
        leftYAxis = lineChart.getAxisLeft();
        rightYAxis = lineChart.getAxisRight();
        //X轴设置显示位置在底部
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setAxisMinimum(0f);
        xAxis.setGranularity(1f);
        //保证Y轴从0开始，不然会上移一点
        leftYAxis.setAxisMinimum(0f);
        rightYAxis.setAxisMinimum(0f);

        /*折线图例 标签 设置*/
        legend = lineChart.getLegend();
        //设置显示类型，LINE CIRCLE SQUARE EMPTY 等等 多种方式，查看LegendForm 即可
        legend.setForm(Legend.LegendForm.LINE);
        legend.setTextSize(12f);
        //显示位置 左下方
        legend.setTextColor(0xFFFFFFFF);
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        //是否绘制在图表里面
        legend.setDrawInside(false);
        //删除X轴的网格线
        xAxis.setDrawGridLines(false);
        //删除Y轴的网格线
        rightYAxis.setDrawGridLines(false);
        leftYAxis.setDrawGridLines(false);
        //设置网格线为虚线
//        leftYAxis.enableGridDashedLine(10f, 10f, 0f);
        //去掉右Y轴
        rightYAxis.setEnabled(false);
        xAxis.setTextColor(0xFFFFFFFF);
        leftYAxis.setTextColor(0xFFFFFFFF);
    }

    public void setLimt(float value){
        LimitLine mLimit=new LimitLine(value) ;
        mLimit.setLineColor(0xFFFFFFFF);
        mLimit.enableDashedLine(10f,10f,0f);
        leftYAxis.addLimitLine(mLimit);
    }

    /**
     * 曲线初始化设置 一个LineDataSet 代表一条曲线
     *
     * @param lineDataSet 线条
     * @param color       线条颜色
     * @param mode
     */
    public void initLineDataSet(LineDataSet lineDataSet, int color, LineDataSet.Mode mode) {
        lineDataSet.setColor(color);
//        设置点的颜色
        lineDataSet.setCircleColor(color);
//        设置数据颜色
        lineDataSet.setValueTextColor(0xFFFFFFFF);
        lineDataSet.setLineWidth(1f);
        lineDataSet.setCircleRadius(3f);
        //设置曲线值的圆点是实心还是空心
        lineDataSet.setDrawCircleHole(false);
        lineDataSet.setValueTextSize(10f);
        //设置折线图填充
        lineDataSet.setDrawFilled(false);
        lineDataSet.setFormLineWidth(1f);
        lineDataSet.setFormSize(15.f);
        if (mode == null) {
            //设置曲线展示为圆滑曲线（如果不设置则默认折线）
            lineDataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);
        } else {
            lineDataSet.setMode(mode);
        }
    }


    /**
     * 展示曲线
     *
     * @param dataList 数据集合
     * @param name     曲线名称
     * @param color    曲线颜色
     */
    public void showLineChart(List<BodyDataInfo> dataList, String name, int color) {
        List<Entry> entries = new ArrayList<>();
        List <Float> valueList = new ArrayList<>();

        for (int i = 0; i < dataList.size(); i++) {
            BodyDataInfo data = dataList.get(i);
            /**
             * 在此可查看 Entry构造方法，可发现 可传入数值 Entry(float x, float y)
             * 也可传入Drawable， Entry(float x, float y, Drawable icon) 可在XY轴交点 设置Drawable图像展示
             */
            Entry entry = new Entry(i, (float) data.getValue());
            entries.add(entry);
            valueList.add(data.getValue());
        }

        if(dataList.size()>0){
            xAxis.setValueFormatter(new ValueFormatter() {
                @Override
                public String getFormattedValue(float value) {
                    String tradeDate = dataList.get((int) value % dataList.size()).getTradeDate();
                    return DateUtil.formatDate(tradeDate);
                }
            });
            Float max = Collections.max(valueList);
            Float min = Collections.min(valueList);
            leftYAxis.setAxisMinimum(min-10); // start at zero
            leftYAxis.setAxisMaximum(max+10); // the axis maximum is 100
        }
        // 每一个LineDataSet代表一条线
        LineDataSet lineDataSet = new LineDataSet(entries, name);
        initLineDataSet(lineDataSet, color, LineDataSet.Mode.CUBIC_BEZIER);
        LineData lineData = new LineData(lineDataSet);
        lineChart.setData(lineData);


    }


}
